package com.kehutong.crm.message.service;

import java.util.List;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.crm.message.entity.Message;
import com.kehutong.crm.message.entity.MessageClassify;

@WebService("/message/classify/service")
public class MessageClassifyService {

    @Inject
    private JdbcSession jdbcSession;

    public Object listPage(JSONObject jsonObject) {

        return Result.success(jdbcSession.findPage(MessageClassify.class)
                                         .like(jsonObject, "name")
                                         .eq("deleted", false)
                                         .order("enable", "desc")
                                         .order("sort", "asc")
                                         .page(jsonObject)
                                         .exe());
    }

    public Object list(JSONObject jsonObject) {

        return Result.success(jdbcSession.findArray(MessageClassify.class)
                                         .eq(jsonObject, "name")
                                         .eq("deleted", false)
                                         .eq("enable", true)
                                         .order("sort", "asc")
                                         .exe());
    }

    public Object save(MessageClassify classify) {
        if (checkDuplicateName(classify.getName(), classify.getId())) {
            return Result.error(0, "存在重复名称");
        }
        jdbcSession.insert(classify);

        return Result.success();
    }

    public Object update(MessageClassify classify) {
        if (checkDuplicateName(classify.getName(), classify.getId())) {
            return Result.error(0, "存在重复名称");
        }

        MessageClassify oldClassify = Pool.get(MessageClassify.class, classify.getId());
        if (!classify.getName().equals(oldClassify.getName())) {
            List<Message> list = jdbcSession.findArray(Message.class)
                                            .eq("classifyId", classify.getId())
                                            .exe();
            list.forEach(e -> {
                e.setClassifyName(classify.getName());
                jdbcSession.updateById(e);
            });
        }

        jdbcSession.updateById(classify);

        return Result.success();
    }


    private boolean checkDuplicateName(String name, String id){

        List<MessageClassify> list = jdbcSession.findArray(MessageClassify.class)
                                                .eq("name", name)
                                                .eq("deleted", false)
                                                .exe();

        return Objects.nonEmpty(list) && !list.get(0).getId().equals(id);
    }

    public Object delete(MessageClassify classify) {
        List<Message> messageList = jdbcSession.findArray(Message.class)
                                               .eq("classifyId", classify.getId())
                                               .eq("deleted", false)
                                               .exe();
        if (messageList.size() > 0) {
            return Result.error(0, "请先删除引用部分");
        }

        classify.setDeleted(true);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object updateSort(MessageClassify classify, Integer sort) {
        classify.setSort(sort);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object setStatus(MessageClassify classify) {
        classify.setEnable(!classify.isEnable());
        jdbcSession.updateById(classify);

        return Result.success();
    }

}
